home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DOS.SWG / 0019_Self-modifying Batch File.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  4KB  |  149 lines

  1. LARS FOSDAL
  2.  
  3. > Hi all.  I've got a little Program that brings up a Window and several
  4. > buttons in TP 7.  The buttons have the names of Various batch Files on them
  5. > which are executed when they are pressed.  The batch Files start up Various
  6. > other Programs.  This launchpad requires about 100K of RAM as currently
  7. > written, and I'm wondering about ways to reduce this amount significantly.
  8. > According to the BP 7 manual resource Files can be used to reduce RAM by 8-
  9. > 10%.  Right now the Various buttons' Labels and commands are stored in
  10. > simple Arrays, which are not the most efficient memory-wise, but I don't
  11. > think that making them Records will significantly reduce RAM need.  I'd like
  12. > to reduce RAM usage an order of magnitude, to about 10K.  Any chance of
  13. > doing this?
  14.  
  15. There is a dirty way of doing this, and it works With every Dos /
  16. command-interpreter that I've tried it under, including Dos 6.0
  17. in a Window under Windows, and 4Dos.
  18.  
  19. The Really nice thing about this way to do it, is that you can even
  20. load TSR's etc. since the menu Program is not in memory at all and there is
  21. no secondary command interpreter when the user executes his choice.
  22.  
  23. The trick is that you run your Program from a "self-modifying" batchFile.
  24.  
  25. --- MENU.BAT ---
  26. :StartAgain
  27. SET MENU=C:\Dos\MENU.BAT  ; Check this environment Var from your menu-prog
  28. GOMENU.EXE                ; and abort if it is not set
  29. SET MENU=
  30. ----------------
  31.  
  32. Lets say you want to run another batchFile from a menu choice f.x MY.BAT.
  33. Let your Program modify the MENU.BAT to:
  34. ---
  35. :StartAgain
  36. SET MENU=C:\Dos\MENU.BAT
  37. GOMENU.EXE
  38. SET MENU=
  39. CALL MY.BAT
  40. GOTO StartAgain
  41. ---
  42.  
  43. When you want to terminate your menu-loop, simply modify the MENU.BAT
  44. back to it's original state.
  45.  
  46. The menu Program can be shared from a network server.  There is no
  47. limitations at all.  You can do Dos commands from the menu Without
  48. having to load a second shell.
  49.  
  50. Following my .sig there is a short example Program.  It can't be run
  51. directly since it Uses some libraries of mine, but you'll get an idea
  52. of how to do it.
  53.  
  54.  
  55. Program HitAndRun; {Menusystem}
  56. Uses
  57.   Dos, Crt, LFsystem, LFCrt, LFinput;
  58. {
  59.   Written by Lars Fosdal
  60.   May 5th, 1991
  61.  
  62.   Released to the public domain, May 15th, 1993
  63. }
  64. Const
  65.   HitAndRunMsg = 'Written by Lars Fosdal ';
  66.   Prog         = 'HIT&RUN';
  67.  
  68. Var
  69.   path : String;
  70.  
  71. {----------------------------------------------------------------------------}
  72.  
  73. Procedure Message(MessageIndex : Integer);
  74. begin
  75.   Writeln(Output);
  76.   Writeln(Output, Prog, ' - ', HitAndRunMsg);
  77.   Write(Output, 'Error: ');
  78.   Case MessageIndex OF
  79.     -1 :
  80.       begin
  81.         Write(Output, Prog, ' must be started from ');
  82.         Writeln(Output,Path + 'MENU.BAT');
  83.       end;
  84.   end;
  85.   Write(Output,^G);
  86. end;
  87.  
  88. Procedure BuildBatchFile(Execute : String);
  89. Var
  90.   BatchFile : Text;
  91. begin
  92.   Assign(BatchFile, Path + 'MENU.BAT');
  93.   ReWrite(BatchFile);
  94.   Writeln(BatchFile, '@ECHO OFF');
  95.   Writeln(BatchFile, 'REM ' + Prog + ' Menu Minder');
  96.   Writeln(BatchFile, 'REM ' + HitAndRunMsg);
  97.   Writeln(BatchFile, ':HitAgain');
  98.   Writeln(BatchFile, 'SET H&R=BATCH');
  99.   Writeln(BatchFile, path + 'HIT&RUN');
  100.   if Execute<>'' then
  101.   begin
  102.     Writeln(BatchFile, Execute);
  103.     Writeln(BatchFile, 'GOTO HitAgain');
  104.   end
  105.   else
  106.     Writeln(BatchFile, 'SET H&R=');
  107.   Close(BatchFile);
  108. end;
  109.  
  110. Function InitOK : Boolean;
  111. Var
  112.   OK : Boolean;
  113. begin
  114.   path   := BeforeLast('\', ParamStr(0)) + '\';
  115.   OK     := GetEnv('H&R') = 'BATCH';
  116.   InitOK := OK;
  117. end;
  118.  
  119. Procedure HitAndRunMenu;
  120. Var
  121.   Mnu : aMenu;
  122.   win : aWindow;
  123. begin
  124.   wDef(Win, 70, 1, 80, 25, 1, Col(Blue, LightGray), Col(Blue, White));
  125.   ItemSeparator:= '`';
  126.   mBarDefault := Red * 16 + Yellow;
  127.   mNew(Mnu, 'Pick an item to run',
  128.        'Quit Menu`COMMAND`DIR /P`D:\BIN\NI'
  129.       + '`D:\BIN\MAPMEM`D:\BIN\X3\XTG'
  130.       + '`D:\BIN\LIST C:\Dos\MENY.BAT');
  131.   Menu(Win, Mnu);
  132.   Case Mnu.Entry OF
  133.     1 : BuildBatchFile('');
  134.     else
  135.       BuildBatchFile(Mnu.Items[Mnu.Entry]^);
  136.   end;
  137. end;{HitAndRunMenu}
  138.  
  139. begin
  140.   if InitOK then
  141.     HitAndRunMenu
  142.   else
  143.   begin
  144.     Message(-1);
  145.     BuildBatchFile('');
  146.   end;
  147.   Writeln(OutPut);
  148. end.
  149.